package cj.web.marriage.controllers;

import java.io.File;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.ImageUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.web.admin.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.marriage.constants.MarriagePhotoRegStatusEnum;
import cj.web.marriage.dao.MarriagePhotoRefDAO;
import cj.web.marriage.domain.MarriagePhotoRef;

@Path("/marriageImage")
public class UploadImageController extends BaseController{

	public final static String image=".jpg";
	
	@Autowired
	private MarriagePhotoRefDAO marriagePhotoRefDAO;
	
	@Get("show")
	public String show(Model model,MarriagePhotoRef marriagePhotoRef)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriageschw);
        model.add("StatusEnum",EnumSet.allOf(MarriagePhotoRegStatusEnum.class));
		return "/views/marriage/marriagePhotoRefList.vm";
	}
	
	@Post("getList")
	public String queryList(MarriagePhotoRef marriagePhotoRef) {
		JSONObject jo = new JSONObject();
		
		List<MarriagePhotoRef> objs = marriagePhotoRefDAO.queryList(marriagePhotoRef);
		
		int size = marriagePhotoRefDAO.querySize(marriagePhotoRef);
		jo.put("data", objs);
		jo.put("draw", marriagePhotoRef.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@"+jo.toString();
	}
	
	@Get("uplaod")
	public String uplaod(Model model){
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriageschw);
		return "/views/marriage/marriageImage.vm";
	}
	
	//老方法
	@Post("doUpload")
    public String doUpload(@Param("uploadimage") MultipartFile[] files,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
    	String path = ReadConfigProperties.getValue("uploadFilePath_Marriage");// 文件保存路径  
		
		// 判断文件列表是否为空  
        if (files != null && files.length > 0) {
        	for(MultipartFile file : files){
        		try {  
        			String fileName =  file.getOriginalFilename(); //文件名
        			String newFileName = fileName.substring(0,fileName.lastIndexOf("."))+image;
                    file.transferTo(new File(path+"/"+newFileName));  // 转存文件  
                    
                    ImageUtils.scale2(path+"/"+newFileName, path+"/s_"+newFileName, 1230, 750, true);//缩率图
                    
                } catch (Exception e) {  
                    e.printStackTrace();  
                    jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
                }  
        	}
            
        } 
		
		return "@"+jo.toString();
    }
	
	//新方法
	@Get("uplaodnew")
	public String uplaodnew(Model model){
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriageschw);
		return "/views/marriage/marriageImagenew.vm";
	}
	
	//新方法
	@Post("doUploadnew")
    public String doUploadnew(@Param("uploadimage") MultipartFile[] files,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
    	String path = ReadConfigProperties.getValue("uploadFilePath_Marriage");// 文件保存路径  
		
		// 判断文件列表是否为空  
        if (files != null && files.length > 0) {
        	for(MultipartFile file : files){
        		try {  
	            	//判断文件是否存在，不存在创建文件
	            	Map rep = ReturnUUID.getFolder(path);
	            	//所在的全路径
	            	path = rep.get("allpath").toString();
	            	//文件名
	                String fileName =  file.getOriginalFilename(); 
	                //统一转换为JPG
	                String newFileName = fileName.substring(0,fileName.lastIndexOf("."))+image;
	                //转存文件  
	                file.transferTo(new File(path+"/"+newFileName));  
	                //JSON 文件路径
	                jo.put("filePath", rep.get("filepath")+"/"+newFileName);
	                //转存
	                ImageUtils.scale2(path+"/"+newFileName, path+"/s_"+newFileName, 1230, 750, true);//缩率图
	                //缩略图路径
	                String slt = rep.get("filepath")+"/s_"+newFileName;
	                
	                String code = fileName.substring(0,fileName.lastIndexOf("."));
	                MarriagePhotoRef obj = marriagePhotoRefDAO.queryByCode(code);
	                if(obj!=null){
	                	//存在
	                	obj.setPhoto(slt);
	                	marriagePhotoRefDAO.update(obj);
	                }else{
	                	//不存在
	                	MarriagePhotoRef marriagePhotoRef = new MarriagePhotoRef();
		                marriagePhotoRef.setCode(code);
		                marriagePhotoRef.setPhoto(slt);
		                marriagePhotoRef.setStatus(MarriagePhotoRegStatusEnum.wfs.getValue());
		                marriagePhotoRefDAO.create(marriagePhotoRef);
	                }
                } catch (Exception e) {  
                    e.printStackTrace();  
                    jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
                }  
        	}
            
        } 
		
		return "@"+jo.toString();
    }
	

}
